package org.example.Server.strategy.question;

import org.example.Server.mapper.question.QuestionMapper;
import org.example.Server.model.question.Option;
import org.example.Server.model.question.QUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 选择题的更新策略实现类
 */
//@Component
public class SelQsUpdateStrategy implements QsUpdateStrategy {
    //        @Autowired
//    QuestionMapper questionMapper;
    private final QuestionMapper questionMapper;

    public SelQsUpdateStrategy(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    @Override
    public void qsUpdate(QUpdate qs) {
        // 更新选项
        List<Option> updateOptions = qs.getUpdate_options();
        if (updateOptions != null && !updateOptions.isEmpty()) {
            questionMapper.updateOption(updateOptions);
            questionMapper.updateQSandOption(qs);
        }
        // 插入选项
        List<Option> insertOptions = qs.getOptions();
        if (insertOptions != null && !insertOptions.isEmpty()) {
            questionMapper.insertOption(insertOptions);
            questionMapper.insertQSandOption(qs);
        }
        // 删除选项
        List<Option> deleteOptions = qs.getDelete_options();
        if (deleteOptions != null && !deleteOptions.isEmpty()) {
            questionMapper.deleteOption(deleteOptions);
        }
    }
}
